// pages/judge/dashboard/dashboard.js
const app = getApp();

Page({
  data: {
    judgeInfo: {
      name: "",
      title: "",
      department: "",
    },
    loginTime: "",
    stats: {
      total: 0,
      pending: 0,
      completed: 0,
      avgScore: 0,
    },
    currentFilter: "all",
    applicationList: [],
  },

  onLoad() {
    this.loadJudgeInfo();
    this.loadStats();
    this.loadApplicationList();
  },

  onShow() {
    console.log("Dashboard onShow 被调用");
    this.loadStats();
    this.loadApplicationList();
  },

  loadJudgeInfo() {
    const userInfo =
      app.globalData.userInfo || wx.getStorageSync("userInfo") || {};
    console.log("加载评委信息:", userInfo);
    console.log("评委部门:", userInfo.profile && userInfo.profile.department);
    this.setData({
      judgeInfo: userInfo,
      loginTime: this.formatTime(new Date()),
    });
  },

  formatTime(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    const hours = String(date.getHours()).padStart(2, "0");
    const minutes = String(date.getMinutes()).padStart(2, "0");
    return `${year}-${month}-${day} ${hours}:${minutes}`;
  },

  async loadStats() {
    try {
      console.log("开始加载统计数据...");
      const local = require("../../../utils/local");
      const dept =
        (this.data.judgeInfo &&
          this.data.judgeInfo.profile &&
          this.data.judgeInfo.profile.department) ||
        "书院";
      console.log("统计部门:", dept);
      const result = local.getJudgeStatsByDepartment(dept);
      console.log("统计数据结果:", result);
      if (result.success) {
        console.log("统计数据:", result.data);
        this.setData({ stats: result.data });
      }
    } catch (error) {
      console.error("加载统计数据失败:", error);
    }
  },

  // 远程统计已移除，改为本地

  async loadApplicationList() {
    try {
      console.log("开始加载申请列表...");
      console.log("当前judgeInfo:", this.data.judgeInfo);
      const local = require("../../../utils/local");
      const dept =
        (this.data.judgeInfo &&
          this.data.judgeInfo.profile &&
          this.data.judgeInfo.profile.department) ||
        "书院";
      console.log("评委部门:", dept, "筛选条件:", this.data.currentFilter);

      // 检查当前用户
      const currentUser = local.getCurrentUser();
      console.log("当前登录用户:", currentUser);

      const result = local.getJudgeApplications(this.data.currentFilter, dept);
      console.log("获取申请结果:", result);
      if (result.success) {
        console.log(`成功获取${result.data.length}条申请`);
        this.setData({ applicationList: result.data });
      } else {
        console.log("获取申请失败:", result.message);
      }
    } catch (error) {
      console.error("加载申请列表失败:", error);
    }
  },

  // 远程申请列表已移除，改为本地

  setFilter(e) {
    const filter = e.currentTarget.dataset.filter;
    this.setData({ currentFilter: filter });
    this.loadApplicationList();
  },

  viewApplication(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/judge/review/review?id=${id}`,
    });
  },

  batchReview() {
    wx.navigateTo({
      url: "/pages/judge/batch/batch",
    });
  },

  exportData() {
    wx.showLoading({ title: "导出中..." });

    // 模拟导出过程
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: "导出成功",
        icon: "success",
      });
    }, 2000);
  },
});
